package com.hospital.dao.impl;

import com.hospital.dao.DepartmentDao;
import com.hospital.model.Department;
import com.hospital.util.DruidUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 科室数据访问对象实现类
 * 实现对科室信息的数据库操作，包括查询所有科室信息等功能
 */
public class DepartmentDaoImpl implements DepartmentDao {
    
    @Override
    public List<Department> findAll() throws SQLException {
        String sql = "SELECT * FROM Department";
        List<Department> departments = new ArrayList<>();
        
        try (Connection conn = DruidUtil.getConnection();
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(sql)) {
            
            while (rs.next()) {
                departments.add(extractFromResultSet(rs));
            }
        }
        return departments;
    }

    /**
     * 从ResultSet中提取科室信息
     * @param rs 包含科室信息的ResultSet对象
     * @return 提取的Department对象
     * @throws SQLException 如果数据库访问出错
     */
    private Department extractFromResultSet(ResultSet rs) throws SQLException {
        Department department = new Department();
        department.setDeptId(rs.getInt("deptId"));
        department.setDeptName(rs.getString("deptName"));
        department.setDeptPhone(rs.getString("deptPhone"));
        department.setDeptDes(rs.getString("deptDes"));
        return department;
    }
} 